﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace PRPSChart
{
    public struct XYZ
    {
        public XYZ(float x, float y, float z)
        {
            X = x;
            Y = y;
            Z = z;
        }

        public float X;
        public float Y;
        public float Z;
    }

    public class Grid
    {

        public double RotateAngle;
       
        public Color Color1 = Color.DarkGray;
        public Color Color2 = Color.Silver;
        public Color Color3 = Color.Silver;
        public Color LineColor = Color.White;

        public Font Font;
        public PointF Location;
        protected XYZ Pts;
        protected float BarWidth;

        public void Init(PointF loc, XYZ pts)
        {
            Location = loc;
            Pts = pts;
            BarWidth = (GetMappedLocation(50, 0).X - Location.X) / 50 - 1; 
            
        }
        
        public void DrawBar(Graphics g, float x, float y, float z, Color color)
        {
            if (z <= 0)
                return;

            PointF loc = GetMappedLocation(x, y);

            if (z >= 1)
            {
                z = 1;
            }

            using (Pen pen = new Pen(color, BarWidth))
            {
                g.DrawLine(pen, new PointF(loc.X + BarWidth , loc.Y), new PointF(loc.X + BarWidth , loc.Y - z * Pts.Z));
            }

        }


        public PointF GetMappedLocation(float x, float y)
        {
            float y0 = (float)(y / 50 * Pts.Y);
            float x0 = (float)(x / 72 * Pts.X);

            float y1 = y0 + 0.8f * x0;
            float x1 = x0;

            return new PointF(Location.X + (float)(x1 * 0.5 + y1 * 0.866),
                                Location.Y + (float)(x1 * -0.866 + y1 * 0.5));

        }

        public PointF GetMappedLocation(PointF p)
        {
            return GetMappedLocation(p.X, p.Y);
        }

        public void DrawString(Graphics g)
        {
            using (SolidBrush brush = new SolidBrush(Color.White))
            {
                g.DrawString(" 0", this.Font, brush, GetMappedLocation(-5f, -3f));
                g.DrawString("10", this.Font, brush, GetMappedLocation(-5f, 7f));
                g.DrawString("20", this.Font, brush, GetMappedLocation(-5f, 17f));
                g.DrawString("30", this.Font, brush, GetMappedLocation(-5f, 27f));
                g.DrawString("40", this.Font, brush, GetMappedLocation(-5f, 37f));
                g.DrawString("50", this.Font, brush, GetMappedLocation(-5f, 47f));
                g.DrawString("0", this.Font, brush, GetMappedLocation(-4f, 53f));
                g.DrawString("+90", this.Font, brush, GetMappedLocation(18 - 4f, 53f));
                g.DrawString("0", this.Font, brush, GetMappedLocation(36 - 4f, 53f));
                g.DrawString("-90", this.Font, brush, GetMappedLocation(54 - 4f, 53f));
                g.DrawString("0", this.Font, brush, GetMappedLocation(72 - 4f, 53f));
            }
        }

        public void DrawGrid(Graphics g)
        {
            using (SolidBrush sb = new SolidBrush(Color3))
            {

                g.FillPolygon(sb, new PointF[] {
                            Location, 
                            Location - new SizeF(0, Pts.Z),                            
                            GetMappedLocation(72f, 0) - new SizeF(0, Pts.Z), 
                            GetMappedLocation(72f, 0)});

            }


            using (SolidBrush sb1 = new SolidBrush(Color1))
            using (SolidBrush sb2 = new SolidBrush(Color2))
            {
                SolidBrush sb0;

                for (int i = 0; i < 5; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        if ((i + j) % 2 == 0)
                            sb0 = sb1;
                        else
                            sb0 = sb2;


                        g.FillPolygon(sb0, new PointF[] {
                            GetMappedLocation(72f / 4 * j, 50f / 5 * i),
                            GetMappedLocation(72f / 4 * (j + 1), 50f / 5 * i), 
                             GetMappedLocation(72f / 4 * (j + 1), 50f / 5 * (i + 1)),
                            GetMappedLocation(72f / 4 * j, 50f / 5 * (i + 1)),
                           
                            });

                    }
                }

            }

            using (Pen pen = new Pen(new SolidBrush(Color.White)))
            {

                for (int i = 0; i <= 5; i++)
                {
                    g.DrawLine(pen, GetMappedLocation(0, 50f / 5 * i), GetMappedLocation(72f, 50f / 5 * i));
                }

                for (int i = 0; i <= 4; i++)
                {
                    g.DrawLine(pen, GetMappedLocation(72f / 4 * i, 0), GetMappedLocation(72f / 4 * i, 50f));
                    PointF pf = GetMappedLocation(72f / 4 * i, 0);
                    g.DrawLine(pen, pf, pf - new SizeF(0, Pts.Z));
                }

                for (int i = 1; i <= 2; i++)
                {
                    PointF pf = GetMappedLocation(72f, 0);
                    SizeF sf = new SizeF(0, Pts.Z / 2 * i);
                    g.DrawLine(pen, Location - sf, pf - sf);
                }

            }



        }

    }
}
